<?php
class Contacts_lib{

	private $user_id;
	function Contacts_lib()
	{
		$this->CI = &get_instance();
		$this->CI->load->model('contactsmodel');
		$this->CI->load->library('form_validation');
		if (!$this->CI->tank_auth->is_logged_in()) {
			redirect('/auth/login/');
		}
		$this->user_id = $this->CI->tank_auth->get_user_id();
		
	}

	function listContacts()
	{
		$this->CI->load->model('groupmodel');
		// retrevies the group list by user id 
		$groupList = $this->CI->groupmodel->getListById($this->user_id);
		$list = $this->CI->contactsmodel->getListById($this->user_id);
		$data['list']= $list;
		$data['groupList'] = $groupList;
		return $this->CI->load->view('contacts/list', $data, TRUE);
	}
	function menu()
	{
		$data['menu_show']= false;
		$data['menu_list']= true;
		return $this->CI->load->view($this->CI->config->item('SMS_contacts_menu_view'), $data, TRUE);
	}
	function addContact()
	{
		$this->CI->form_validation->set_rules('mobile_no', 'Mobile No', 'trim|required|xss_clean');
		$this->CI->form_validation->set_value('mobile_no');
		$this->CI->form_validation->set_rules('name', 'Name', 'trim|required|xss_clean');
		$this->CI->form_validation->set_value('name');
		$data['errors'] = array();
		$data['add'] = '';
		if($this->CI->form_validation->run())
		{
			$insert = array('user_id'		=> $this->user_id,
							'name'			=> $this->CI->input->post('name',true),
							'mobileno'		=> $this->CI->input->post('mobile_no',true));
			$this->CI->contactsmodel->insert($insert);
			$this->CI->session->set_flashdata('flashMessage','Contact added Successfully');
			redirect('/contacts/add');
		}
		return $this->CI->load->view('contacts/form', $data, TRUE);
	}
}